package com.soujiayi.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.TextView;

import com.soujiayi.util.Constants;

public class ProductDetailWebViewActivity extends Activity {
	
	private WebView webView;
	private TextView goBackView;
	private TextView goForwardView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		((MyApplication) this.getApplication()).addActivity(this);
		getWindow().requestFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.product_detail_webview);
		
		Intent intent = this.getIntent();
		String url = intent.getStringExtra(Constants.PRODUCT_URL);
		
		String productName = intent.getStringExtra(Constants.PRODUCT_NAME);
		if(productName != null && productName.length() > 15){
			productName = productName.substring(0, 15) + "...";
		}
		TextView productNameView = (TextView) findViewById(R.id.product_webview_productName);
		productNameView.setText(productName == null ? "": productName);
		
		webView  = (WebView)  findViewById(R.id.product_webView);  
//        webView.setWebViewClient(new WebViewClient() {
//        	public boolean shouldOverrideUrlLoading(WebView view, String url)
//                { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
//                        view.loadUrl(url);
//                        return true;
//                }
//        });

//        webView.setOnTouchListener(new View.OnTouchListener() {
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//				updateFooter();
//				return false;
//			}
//		});
//		goBackView = (TextView) findViewById(R.id.product_webview_goback);
//		goForwardView = (TextView) findViewById(R.id.product_webview_goforward);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				ProductDetailWebViewActivity.this.setProgress(progress * 100);
			}			
		});
		webView.loadUrl(url);
		
	}

	public void backToHome(View view) {
		Intent intent = new Intent(ProductDetailWebViewActivity.this,
				IndexActivity.class);
		startActivity(intent);
		this.finish();
	}

	public void back(View view) {
		this.finish();
		overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 如果按下的是返回键，并且没有重复
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_right_in,
					R.anim.push_right_out);
			return false;
		}
		return false;
	}
	
	public void webviewGoBack(View view) {
		if(webView.canGoBack()){
			webView.goBack();
		}
		updateFooter();
	}
	
	public void webviewGoForward(View view) {
		if(webView.canGoForward()){
			webView.goForward();
		}
		updateFooter();
	}
	
	public void updateFooter(){
		if(webView.canGoBack()){
			goBackView.setTextColor(getResources().getColor(R.color.white));
		}else{
			goBackView.setTextColor(getResources().getColor(R.color.gray2));
		}
		
		if(webView.canGoForward()){
			goForwardView.setTextColor(getResources().getColor(R.color.white));
		}else{
			goForwardView.setTextColor(getResources().getColor(R.color.gray2));
		}
	}

}
